<?php
class manageModel extends RPHP_Model
{

    protected $_auth  = null;
    protected $_goods = null;

    //检查在线
    public function checkAuth($goto = true, $menu = true)
    {
        $this->_auth = $this->auth->checkSignin($goto, $menu);
        return $this->_auth;
    }

    //检查权限
    public function checkPriv($pv, $return = true, $menu = true)
    {
        $auth = $this->auth->CheckPriv($pv, $return, $menu);
        if ($return) {
            $this->_auth = $auth;
        }

        return $auth;
    }

    //获取首页图表数据
    public function getHomeChartData()
    {
        $time_start = strtotime(date("Y-m-1 0:0:0"));
        $time_end   = $_SERVER['REQUEST_TIME'];

        //会员数据
        $sql  = "SELECT `uid`,`create_time` FROM {$this->_prefix['primary']}member_account WHERE `create_time`>=" . $time_start . " AND `create_time`<=" . $time_end . " AND `stat`<3";
        $data = $this->sql($sql);
        if ($data) {

            foreach ($data as $r) {
                $day = date("j", $r['create_time']);
                $mdays[$day] += 1;
            }

        }

        // 订单数据

        $order['count'] = 0;
        //已付款订单
        $sql  = "SELECT `goods_amount`,`shipping_fee`,`totalaxaprice`,`order_id`,`add_time` FROM {$this->_prefix['primary']}order_info WHERE `pay_status`=2 AND `order_status` NOT IN (2,3)  AND `add_time`>=" . $time_start . " AND `add_time`<=" . $time_end;
        $data = $this->sql($sql);
        if ($data) {
            $order['count'] += count($data);
            foreach ($data as $r) {
                $day = date("j", $r['add_time']);
                $order['payed']['count'][$day] += 1;
                $order['payed']['money'][$day] += $r['goods_amount'];
                $order['sfee']['money'][$day] += $r['shipping_fee'];
                $order['axap']['money'][$day] += $r['totalaxaprice'];
            }

        }
        //未付款订单
        $sql  = "SELECT `goods_amount`,`order_id`,`add_time` FROM {$this->_prefix['primary']}order_info WHERE `pay_status`=0 AND `add_time`>=" . $time_start . " AND `add_time`<=" . $time_end;
        $data = $this->sql($sql);
        if ($data) {
            $order['count'] += count($data);
            foreach ($data as $r) {
                $day = date("j", $r['add_time']);
                $order['unpay']['count'][$day] += 1;
                $order['unpay']['money'][$day] += $r['goods_amount'];

            }

        }

        //会员开通数量
        $sql  = "SELECT `order_id`,`ctime` FROM {$this->_prefix['primary']}member_payinfo WHERE `order_type`=0 AND `pay_status`=2 AND `ctime`>=" . $time_start . " AND `ctime`<=" . $time_end;
        $data = $this->sql($sql);
        if ($data) {

            foreach ($data as $r) {
                $day = date("j", $r['ctime']);
                $days[$day] += 1;
            }

        }

        //订单销售数量
        $sql  = "SELECT add_time from {$this->_prefix['primary']}order_info  where `pay_status`=2 AND  `order_status` NOT IN (2,3)  AND  `add_time`>=" . $time_start . " AND  `add_time`<=" . $time_end;
        $data = $this->sql($sql);
        foreach ($data as $r) {
            $day = date("j", $r['add_time']);
            $order_goods[$day] += 1;
        }

        $year   = date("Y");
        $month  = date("n");
        $maxday = $this->c['month'][$month];
        if ($month == 2 && (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0)) {
            $maxday = 29;
        }

        return array(
            'date'        => date("Y年n月", $time_start),
            'start'       => date("j日", $time_start),
            'end'         => date("j日", $time_end),
            'eofday'      => date("j", $time_end),
            'member'      => $mdays,
            'mpay'        => $days,
            'order'       => $order,
            'maxday'      => $maxday,
            'order_goods' => $order_goods,
        );
    }

    //保存密码
    public function savePassword($cur, $new)
    {

        $code = 900;
        if ($cur != "" && strlen($new) >= 6) {

            $sql = "SELECT `aid`,`password`,`pass_rand` FROM {$this->_prefix['primary']}admin_account WHERE `aid`=" . $this->_auth['aid'] . " AND `stat`<3";
            $rs  = $this->sql($sql);

            if ($rs) {
                $curr = md5(md5($cur) . $rs[0]['pass_rand']);
                if ($curr == $rs[0]['password']) {
                    $new = md5(md5($new) . $rs[0]['pass_rand']);
                    $sql = "UPDATE {$this->_prefix['primary']}admin_account SET `password`='" . $new . "',`last_aid`=" . $this->_auth['aid'] . ",`last_edit`=" . $_SERVER['REQUEST_TIME'] . " WHERE `aid`=" . $this->_auth['aid'];

                    $rd = $this->sql($sql);

                    if ($rd > 0) {
                        $code = 200;
                    } else {
                        $code = 403;
                    }

                } else {
                    $code = 504;
                }

            } else {
                $code = 404;
            }

        }

        return $code;

    }

    //删除在线数据
    public function deleteOnline($token)
    {
        $sql = "DELETE FROM {$this->_prefix['primary']}admin_online WHERE `token`='" . $token . "'";
        $rs  = $this->sql($sql);
        return $rs;
    }

    //获取最后登录信息
    public function getLastSign($aid)
    {

        $sql = "SELECT `log_ip`,`log_time` FROM {$this->_prefix['primary']}admin_signinlog WHERE aid=" . intval($aid) . " ORDER BY `id` DESC LIMIT 2";
        $rs  = $this->sql($sql);
        if ($rs[1]) {
            $rs[0] = $rs[1];
        }

        return $rs[0];
    }

    //获取商品在线及库存警告
    public function getGoodsCount()
    {
        // 在售商品总数
        $sql             = "SELECT COUNT(`goods_id`) AS `cnt` FROM {$this->_prefix['primary']}goods_data WHERE `is_on_sale`=1 AND `is_delete`=0";
        $data['on_sale'] = $this->db->getone($sql);

        // 库存警告数量
        $sql             = "SELECT COUNT(GAN.`goods_id`) AS `cnt` FROM {$this->_prefix['primary']}goods_data AS GD INNER JOIN {$this->_prefix['primary']}goods_stock AS GAN ON GD.`goods_id` = GAN.`goods_id` AND GD.`is_on_sale`=1 AND GD.`is_delete`=0 AND GD.`warn_number`>=GAN.`number`";
        $data['warning'] = $this->db->getone($sql);

        return $data;
    }

    //统计订单数据
    public function getOrderCount()
    {
        // 累积订单数量，累积销售金额
        $sql           = "SELECT SUM(`goods_amount`) AS `money`,COUNT(`order_id`) AS `cnt` FROM {$this->_prefix['primary']}order_info WHERE `pay_status`=2 AND `order_status` NOT IN (2,3)";
        $data['total'] = $this->db->getrow($sql);

        // 今日订单数量，今日订单金额
        $sql           = "SELECT SUM(`goods_amount`) AS `money`,COUNT(`order_id`) AS `cnt` FROM {$this->_prefix['primary']}order_info WHERE `pay_status`=2 AND `order_status` NOT IN (2,3) AND `add_time`>=" . strtotime("today");
        $data['today'] = $this->db->getrow($sql);

        // 统计未发货订单
        $sql                = "SELECT COUNT(`order_id`) AS `cnt` FROM {$this->_prefix['primary']}order_info WHERE `pay_status`=2 AND `order_status`=1 AND `shipping_status`=0";
        $data['unshipping'] = $this->db->getone($sql);

        return $data;

    }

    //获得会员总数量
    public function getMemberCount()
    {
        $sql           = "SELECT COUNT(`uid`) AS `cnt` FROM {$this->_prefix['primary']}member_account WHERE `stat`<3";
        $data['total'] = $this->db->getone($sql);

        $sql           = "SELECT COUNT(`uid`) AS `cnt` FROM {$this->_prefix['primary']}member_account WHERE `create_time`>=" . strtotime("today") . " AND `create_time`>=" . strtotime("today") . " AND `stat`<3";
        $data['today'] = $this->db->getone($sql);

        return $data;
    }

    //获得会员开通数量
    public function getPaidNum()
    {
        // 总开通数
        $sql           = "SELECT COUNT(`order_id`) AS `cnt` FROM {$this->_prefix['primary']}member_payinfo WHERE `order_type` = 0 AND `pay_status`=2";
        $data['total'] = $this->db->getone($sql);

        //今日开通
        $sql           = "SELECT COUNT(`order_id`) AS `cnt` FROM {$this->_prefix['primary']}member_payinfo WHERE `order_type` = 0 AND `pay_status`=2 AND `ctime`>=" . strtotime("today");
        $data['today'] = $this->db->getone($sql);

        return $data;
    }

    // 统计购物车数据
    public function getCartCount()
    {
        // 总商品数
        $sql           = "SELECT COUNT(`id`) AS `cnt` FROM {$this->_prefix['primary']}goods_cart";
        $data['total'] = $this->db->getone($sql);

        //今日加入
        $sql           = "SELECT COUNT(`id`) AS `cnt` FROM {$this->_prefix['primary']}goods_cart WHERE `create_time`>=" . strtotime("today");
        $data['today'] = $this->db->getone($sql);

        return $data;
    }

    public function getBindUid()
    {
        $sql = "SELECT `uid` FROM {$this->_prefix['primary']}admin_account WHERE `aid`=" . $this->_auth['aid'];
        return $this->db->getone($sql);
    }

/*战略合作商*/

    public function getAllAgentGoods()
    {
        $sql = "SELECT * FROM {$this->_prefix['primary']}agent_goods ORDER BY `goods_id` ASC";
        $tmp = $this->sql($sql);
        if ($tmp) {
            foreach ($tmp as $r) {
                $this->_goods[$r['goods_id']] = $r;
            }

            return $this->_goods;
        }
    }

    // 出库
    public function getStockOut($uid)
    {
        $month_time = strtotime(date("Y-m-1"));
        $today_time = strtotime("today");

        $sql = "SELECT `goods_id`,`number`,`create_time` FROM {$this->_prefix['primary']}agent_stock_log WHERE `from_uid`=" . $uid . " AND `create_time`>=" . $month_time;
        $tmp = $this->sql($sql);
        if ($tmp) {
            if ($this->_goods == null) {
                $this->getAllAgentGoods();
            }

            $agent_level = $this->db->getone("SELECT `agent_level` FROM {$this->_prefix['primary']}member_account WHERE `uid`=" . $uid);

            foreach ($tmp as $r) {

                $money = $this->_goods[$r['goods_id']] ? $this->_goods[$r['goods_id']]['price_' . $agent_level] * $r['number'] : 0;

                if ($r['create_time'] >= $today_time) {
                    $today['number'] += $r['number'];
                    $today['money'] += $money;
                }

                $mdays['number'][date("j", $r['create_time'])] += $r['number'];
                $mdays['money'][date("j", $r['create_time'])] += $money;

            }

            return array(
                'today' => $today,
                'month' => $mdays,
            );
        }
    }

    // 今日分润
    public function getSeparateToday($uid)
    {
        $sql = "SELECT SUM(`cmoney`) AS `cnt` FROM {$this->_prefix['primary']}finance_detail WHERE `uid`=" . $uid . " AND `event` IN (3,8) AND `create_time`>=" . strtotime("today");

        return $this->db->getone($sql);
    }

    public function getUserChart($uid)
    {

        $today_time = strtotime("today");
        $month_time = strtotime(date("Y-m-1"));
        $data       = $this->getChilds($uid);
        if ($data) {
            foreach ($data as $time) {
                if ($time >= $today_time) {
                    $today += 1;
                }

                if ($time >= $month_time) {

                    $mdays[date("j", $time)] += 1;
                }
            }

            return array(
                'today' => $today,
                'month' => $mdays,
                'total' => count($data),
            );
        }
    }

    // 获取所有下级用户
    public function getChilds($uid, $data = false)
    {
        $sql = "SELECT `uid`,`create_time` FROM {$this->_prefix['primary']}member_account WHERE `pid`=" . $uid;
        $tmp = $this->sql($sql);

        if ($tmp) {
            foreach ($tmp as $r) {
                $data[$r['uid']] = $r['create_time'];

                $data = $this->getChilds($r['uid'], $data);
            }
        }

        return $data;
    }

}
